using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace FederationMembershipDB
{
	public class Class
	{
		int _ClassID;
		string _Name;
		int _Type;
		DateTime _Date;
		DateTime _Time;
		int _Duration;
		int _Style;

        public int ClassID
        {
            get { return _ClassID; }
            set { _ClassID = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public override string ToString()
        {
            return string.Format("{0} - {1}", _ClassID, _Name);
        }

		public void AddClass(int classid, string name, int type, DateTime date, DateTime time, int duration, int style)
		{
            _ClassID = classid;
            _Name = name;
            _Type = type;
            _Date = date;
            _Time = time;
            _Duration = duration;
            _Style = style;

            //Add to Database
            Utilities.openDatabase();

            SqlCommand insertClass = new SqlCommand(string.Format("INSERT into Class(ID, Name, Type, ClassDate, ClassTime, Duration, Style) VALUES ({0},'{1}',{2},CONVERT(datetime, '{3}', 103),'{4}',{5},{6})",
                _ClassID, _Name, _Type, _Date.ToShortDateString(), _Time.ToShortTimeString(), _Duration, _Style), Utilities.connection);

            insertClass.ExecuteNonQuery();
            Utilities.connection.Close();
		}

        public void UpdateClass(int classid, string name, int type, DateTime date, DateTime time, int duration, int style)
		{
            _ClassID = classid;
            _Name = name;
            _Type = type;
            _Date = date;
            _Time = time;
            _Duration = duration;
            _Style = style;

            //Update Database
            Utilities.openDatabase();

            SqlCommand updateClass = new SqlCommand(string.Format("UPDATE Class  SET Name='{0}', Type={1}, ClassDate=CONVERT(datetime, '{2}', 103), ClassTime='{3}', Duration={4}, Style={5} WHERE ID = {6}",
                _Name, _Type, _Date.ToShortDateString(), _Time.ToShortTimeString(), _Duration, _Style,_ClassID), Utilities.connection);

            updateClass.ExecuteNonQuery();
            Utilities.connection.Close();
		}

        public void DeleteClass(int classid)
        {
            //Delete from database
            Utilities.openDatabase();

            SqlCommand deleteClass = new SqlCommand(string.Format("DELETE FROM Class WHERE ID='{0}'", classid));

            deleteClass.ExecuteNonQuery();
            Utilities.connection.Close();
        }

        public bool isValid()
        {
            bool isValid = false;

            DataSet Class = Utilities.SearchDatabase(String.Format("SELECT * FROM CLASS WHERE ID = {0}", _ClassID));
            if (Class.Tables[0].Rows.Count == 1)
            {
                isValid = true;
            }

            return isValid;
        }

        
	}
}
